using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TemplateWizard;
using System.Windows.Forms;
using EnvDTE;

namespace SPVisualDev.VSAddin
{
    public class NewWebPartDefinitionWizard: IWizard
    {
        private DTE m_DTE;

        public NewWebPartDefinitionWizard()
        {
        }

        // This method is called before opening any item that 
        // has the OpenInEditor attribute.
        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(Project project)
        {
        }

        // This method is only called for item templates,
        // not for project templates.
        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
        }

        // This method is called after the project is created.
        public void RunFinished()
        {
        }

        public void RunStarted(object automationObject,
            Dictionary<string, string> replacementsDictionary,
            WizardRunKind runKind, object[] customParams)
        {
            /*
            try
            {
                //System.Diagnostics.Debugger.Break();

                m_DTE = (DTE)automationObject;

                string safe_item_name = replacementsDictionary["$rootname$"];
                if (safe_item_name.IndexOf(".") != -1)
                    safe_item_name = safe_item_name.Split('.')[0];

                safe_item_name = safe_item_name.Replace(".", "_");

                if (m_DTE.SelectedItems.Count > 0)
                {
                    Project proj = m_DTE.SelectedItems.Item(1).Project;
                    if (proj == null)
                    {
                        proj = m_DTE.SelectedItems.Item(1).ProjectItem.ContainingProject;
                    }

                    string default_ns = proj.Properties.Item("DefaultNamespace").Value.ToString();
                    string asm_name = Common.GetTargetBuildAssemblyName(proj, false);
                    string custom_ns = Common.GetProjectGlobalConfigKey(proj, "WebPartsNSsuffix");

                    replacementsDictionary.Add("$assembly$", asm_name);
                    replacementsDictionary.Add("$namespace$", default_ns + "." + custom_ns);

                    if (asm_name.ToLower().IndexOf("version=") != -1)
                        replacementsDictionary.Add("$typename$", default_ns + "." + custom_ns + "." + safe_item_name + ", " + asm_name);
                    else
                        replacementsDictionary.Add("$typename$", default_ns + "." + custom_ns + "." + safe_item_name);
                }

            }
            catch (Exception ex)
            {
#if DEBUG
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
                throw new WizardBackoutException();
            }
            */
        }

        // This method is only called for item templates,
        // not for project templates.
        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}
